package com.mtnz.njk.Service;

import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageHelper;
import com.mtnz.njk.Mapper.*;
import com.mtnz.njk.entity.*;
import com.mtnz.njk.result.PageInfo;
import com.mtnz.njk.result.ResponseMessage;
import com.mtnz.njk.result.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class ApplyVipManagerService {
    @Autowired
    ApplyVipMapper applyVipMapper;

    @Autowired
    ApplyVipManagerMapper applyVipManagerMapper;

    @Autowired
    NjkUserMapper userMapper;

    @Autowired
    ProductMapper productMapper;

    @Autowired
    NjkUserServiceMapper userServiceMapper;

    @Autowired
    NjkUserInfoMapper userInfoMapper;

    @Autowired
    ProductPriceAuthorityMapper productPriceAuthorityMapper;

    /**
     * vip申请查看
     *
     * @param applyStatus
     * @param serviceId
     * @param key
     * @param pageNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    public ResponseResult listApplyVip(Integer applyStatus, String serviceId, String key, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) throws Exception {
        ArrayList<String> userIds = new ArrayList<>();
        if (serviceId != null && !("").equals(serviceId)) {
            // 查询此客服的用户
            Example userExample = new Example(NjkUserEntity.class);
            userExample.createCriteria().andEqualTo("serviceId", serviceId);
            List<NjkUserEntity> njkUserEntities = userMapper.selectByExample(userExample);
            for (NjkUserEntity njkUserEntity : njkUserEntities) {
                userIds.add(njkUserEntity.getUserId());
            }
        }
        // vip申请查看列表
        PageHelper.startPage(pageNum, pageSize);
        List<ApplyVipEntity> applyVip = applyVipManagerMapper.getApplyVip(applyStatus, userIds, key);
        PageInfo<ApplyVipEntity> applyVipEntityPageInfo = new PageInfo<>(applyVip);
        List<ApplyVipEntity> list = applyVipEntityPageInfo.getList();

        // 意向产品信息
        for (ApplyVipEntity applyVipEntity : list) {
            if (applyVipEntity.getProductId() != null && !("").equals(applyVipEntity.getProductId())) {
                ArrayList<ProductEntity> productEntities = new ArrayList<>();
                String productId = applyVipEntity.getProductId();
                String[] split = productId.split(",");
                List<String> productIds = Arrays.asList(split);
                for (String id : productIds) {
                    ProductEntity productEntity = productMapper.selectByPrimaryKey(id);
                    productEntities.add(productEntity);
                }
                // 向申请信息中置入产品信息
                applyVipEntity.setProductEntity(productEntities);
            }
        }

        // 加入负责人信息
        // 将负责人姓名置入申请信息中
        for (ApplyVipEntity applyVipEntity : list) {
            String userId = applyVipEntity.getUserId();
            // 查询用户信息
            NjkUserEntity userEntity = userMapper.selectByPrimaryKey(userId);
            if (userEntity != null) {
                // 查询客服信息
                NjkUserServiceEntity njkUserServiceEntity = userServiceMapper.selectByPrimaryKey(userEntity.getServiceId());
                if (njkUserServiceEntity != null) {
                    applyVipEntity.setServiceName(njkUserServiceEntity.getName());
                }
            }
        }

        // 将用户的职业信息置入申请信息中
        for (ApplyVipEntity applyVipEntity : list) {
            String userId = applyVipEntity.getUserId();
            Example userInfoExample = new Example(NjkUserInfoEntity.class);
            userInfoExample.createCriteria().andEqualTo("userId", userId);
            NjkUserInfoEntity njkUserInfoEntity = userInfoMapper.selectOneByExample(userInfoExample);
            if (njkUserInfoEntity != null) {
                applyVipEntity.setJob(njkUserInfoEntity.getJob());
            }
        }
        // 这个客服没有客户
        if (serviceId != null && !("").equals(serviceId) && (userIds == null || userIds.size() == 0)) {
            applyVipEntityPageInfo.setPages(0);
            applyVipEntityPageInfo.setTotal(0);
            applyVipEntityPageInfo.setList(null);
        }
        return new ResponseResult(ResponseMessage.OK, applyVipEntityPageInfo);
    }

    /**
     * 拒绝提交或者提交审核或者审核拒绝或者审核通过
     *
     * @param applyStatus 1 审核通过 2 申请中 3 被拒提交 4 提交 （审核中）
     * @param applyVipId
     * @return
     * @throws Exception
     */
    public ResponseResult auditApplyVip(Integer applyStatus, String applyVipId,String productIds) throws Exception {
        ApplyVipEntity applyVipEntity = new ApplyVipEntity();
        applyVipEntity.setApplyVipId(applyVipId);
        applyVipEntity.setApplyStatus(applyStatus);
        if(productIds != null && !("").equals(productIds)){
            applyVipEntity.setProductId(productIds);
        }
        int i = applyVipMapper.updateByPrimaryKeySelective(applyVipEntity);

        if(applyStatus == 1){
            // 审核通过，给用户增加商品权限
            ApplyVipEntity applyVip = applyVipMapper.selectByPrimaryKey(applyVipId);
            String userId = applyVip.getUserId();
            String productIdes = applyVip.getProductId();
            String[] split = productIdes.split(",");
            for (String productId : split) {
                String id = IdUtil.simpleUUID();
                ProductPriceAuthorityEntity productPriceAuthorityEntity = new ProductPriceAuthorityEntity();
                productPriceAuthorityEntity.setAuthorityId(id);
                productPriceAuthorityEntity.setProductId(productId);
                productPriceAuthorityEntity.setUserId(userId);
                productPriceAuthorityMapper.insertSelective(productPriceAuthorityEntity);
            }

        }
        if (i > 0)
            return new ResponseResult(ResponseMessage.OK);
        return new ResponseResult(ResponseMessage.FAIL);
    }
}
